package com.example.chapter05;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "CartServlet", value = "/CartServlet")
public class CartServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType( "text/html;charset=utf-8" );
        request.setCharacterEncoding("utf-8");
        PrintWriter out =  response.getWriter() ;
        out.print("<P>这里是结账处,您的姓名以及选择的商品是：<br />");
        HttpSession session =  request.getSession() ;
        String personName = (String)request.getSession().getAttribute  ("customerName");
        out.println("<br>您的姓名：" +  personName);
        Enumeration enumGoods = session.getAttributeNames();
        out.println("<br>购物车中的商品：<br>");
        while (enumGoods.hasMoreElements()) {
            String key = (String) enumGoods. nextElement();
            String goods = (String) session.getAttribute( key);
            if (!(goods.equals(personName)))
                out.println( goods+ "<br>");
        }

        out.println("<P>点击超链接，链接到food.html的页面，购买食品。");
        out.println("<A href='food.html'>  欢迎去food.html！</A>");
        out.println("<P>点击超链接，链接到login.html的页面,去修改姓名。");
        out.println("<A href='login.html'>  欢迎去login.html！</A>");
        out.println("<P>点击超链接，链接到logout页面,退出");
        out.println("<A href='/chapter05_war_exploded/LogOutServlet'> 退出！</A>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}